package com.yudean.esb.service.impl.erp;

import com.yudean.esb.dao.erp.ErpIdsInsHeaderIntDao;
import com.yudean.esb.domain.erp.ErpIdsInsHeaderInt;
import com.yudean.esb.service.ErpIdsInsHeaderIntService;
import com.yudean.esb.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.List;

/**
 * Created by zenghui on 2015-05-18.
 * desc:
 */
@WebService(endpointInterface = "com.yudean.esb.service.ErpIdsInsHeaderIntService", serviceName = "ErpIdsInsHeaderIntService",targetNamespace = "http://service.esb.yudean.com/")
@Service
@Scope("prototype")
public class ErpIdsInsHeaderIntServiceImpl implements ErpIdsInsHeaderIntService {
    private static final String TAG = "ErpIdsInsHeaderIntServiceImpl";
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ErpIdsInsHeaderIntServiceImpl.class);
    private static final String SERVICE_NAME = "ERP-保险";
    private static final String SERVICE_TYPE = "发布服务";



    @Resource
    private ErpIdsInsHeaderIntDao erpIdsInsHeaderIntDao;

    @Override
    public String getInsuranceData(String compCodeStr,
                                   String startDateStr,
                                   String endDateStr,
                                   String insTypeStr,
                                   String insNumberStr) {
        List<ErpIdsInsHeaderInt> list = erpIdsInsHeaderIntDao.getInsuranceData(compCodeStr,
                startDateStr,
                endDateStr,
                insTypeStr,
                insNumberStr);
        // 没有数据的情况下：返回空串
        if (list == null || list.size() < 1) {
            logger.debug("getInsuranceData():no data");
            return this.noData("no data");
        }
        logger.debug("getInsuranceData():list:{size:{}}", list.size());
        return this.parseList(list);
    }

    /**
     * 转换List为XML字符串
     *
     * @param list
     * @return
     */
    private String parseList(List<ErpIdsInsHeaderInt> list) {
        Document doc = null;
        doc = DocumentHelper.createDocument();
        Element root = doc.addElement("service");
        root.addElement("name").setText(SERVICE_NAME);
        root.addElement("type").setText(SERVICE_TYPE);
        Element dataList = root.addElement("data");
        try {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                ErpIdsInsHeaderInt erpIdsInsHeaderInt = list.get(i);
                if (erpIdsInsHeaderInt == null) {
                    continue;
                }
                Element data = dataList.addElement("item");
                this.dataWrapper(erpIdsInsHeaderInt, data);
            }
            return doc.asXML();
        } catch (Exception e) {
            logger.error("parseList():error:" + e.getMessage(), e);
            return this.noData("parse error");
        }
    }
    /**
     * 将对象装配到Elment中去
     *
     * @param erpIdsInsHeaderInt
     * @param data
     */
    private void dataWrapper(ErpIdsInsHeaderInt erpIdsInsHeaderInt, Element data) {
        data.addElement("compCode").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getCompCode()));
        data.addElement("insNumber").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsNumber()));
        data.addElement("endOrSementNum").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getEndOrSementNum()));
        data.addElement("insType").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsType()));
        data.addElement("contractCode").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getContractCode()));
        data.addElement("contractName").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getContractName()));
        data.addElement("startDate").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getStartDate()));
        data.addElement("endDate").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getEndDate()));
        data.addElement("insStatus").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsStatus()));
        data.addElement("currency").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getCurrency()));
        data.addElement("insAssetAmount").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsAssetAmount()));
        data.addElement("insAmount").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsAmount()));
        data.addElement("indemnityLimit").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getIndemnityLimit()));
        data.addElement("insPsnNum").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsPsnNum()));
        data.addElement("perPsnAmount").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getPerPsnAmount()));
        data.addElement("feeRate").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getFeeRate()));
        data.addElement("insFeeCny").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsFeeCny()));
        data.addElement("insFeeForeign").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getInsFeeForeign()));
        data.addElement("exchangeRate").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getExchangeRate()));
        data.addElement("feeStatus").setText(
                StringUtil.convertToString(erpIdsInsHeaderInt.getFeeStatus()));


    }

    /**
     * 返回空数据的时候的信息
     *
     * @return
     */
    private String noData(String errorInfo) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<service>" +
                "<name>"+SERVICE_NAME+"</name>" +
                "<type>"+SERVICE_TYPE+"</type>" +
                "<data>" +
                "<!--"+errorInfo+"-->" +
                "</data>" +
                "</service>";
    }
}
